﻿#region License
/*Copyright (c) 2010 Andrew Collard (andrewcollard@hotmail.com)

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in
all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
THE SOFTWARE.*/
#endregion

using System.Collections;
using System.ComponentModel;
using System.Windows.Data;
using TorchView.CharacterEditor.Services;
using TorchView.Composition;
using System.Windows.Input;
using TorchView.Windows;
using TorchView.Game;
using System.Threading.Tasks;
using Microsoft.Win32;
using System.Windows;
using System.Windows.Threading;
using System;
using System.IO;

namespace TorchView.CharacterEditor
{
  /// <summary>
  /// View model for the character editor
  /// </summary>
  public sealed class CharacterEditorViewModel : INotifyPropertyChanged, ICharacterEditorViewModel
  {
    private ListCollectionView _GamesView;
    private IGamesProvider _GamesProvider;
    private IGameExporter _GameExporter;
    private IMessageBoxService _MessageBoxService;

    public CharacterEditorViewModel(IView<ICharacterEditorViewModel> view, IGamesProvider gamesProvider, IGameExporter gameExporter, IMessageBoxService messageBoxService)
    {
      View = view;
      _GamesView = new ListCollectionView(gamesProvider.Games as IList);
      _GamesProvider = gamesProvider;
      _GameExporter = gameExporter;
      _MessageBoxService = messageBoxService;
      ExportSavedGameCommand = new DirectCommand(ExportGameFile);
      ReloadGamesCommand = new DirectCommand(ReloadGames);
      View.Model = this;
    }

    private void ExportGameFile(object parameter)
    {
      Dispatcher mainDispatcher = Dispatcher.CurrentDispatcher;
      var savedGame = _GamesView.CurrentItem as SaveGame;
      SaveFileDialog dialog = new SaveFileDialog();
      dialog.DefaultExt = "xml";
      dialog.Filter = "*.xml|*.xml";
      dialog.FileName = savedGame.MainCharacter.Name + ".xml";
      dialog.Title = "Export Save Game";
      if (dialog.ShowDialog() ?? false)
      {
        Task.Factory.StartNew(() => _GameExporter.Export(savedGame, dialog.FileName))
          .ContinueWith(t => mainDispatcher.BeginInvoke(new Action(() => _MessageBoxService.Show("Save Game export completed.", "Export Completed", MessageBoxButton.OK, MessageBoxImage.Information, MessageBoxResult.OK))));
      }
    }

    private void ReloadGames(object parameter)
    {
      _GamesProvider.Reload();
      SetGames(new ListCollectionView(_GamesProvider.Games as IList));
    }

    /// <summary>
    /// Get the loaded game files
    /// </summary>
    public IEnumerable Games
    {
      get { return _GamesView; }
    }

    public IView<ICharacterEditorViewModel> View { get; private set; }
    public ICommand ExportSavedGameCommand { get; private set; }
    public ICommand ReloadGamesCommand { get; private set; }

    /// <summary>
    /// Event raised when a property changes
    /// </summary>
    public event PropertyChangedEventHandler PropertyChanged;

    /// <summary>
    /// Update the games property
    /// </summary>
    /// <param name="games">Collection of games</param>
    private void SetGames(ListCollectionView games)
    {
      _GamesView = games;
      OnPropertyChanged("Games");
    }

    /// <summary>
    /// Helper method to raise the property changed event
    /// </summary>
    /// <param name="property">Name of the property that changed</param>
    private void OnPropertyChanged(string property)
    {
      var handler = PropertyChanged;
      if (handler != null)
        handler(this, new PropertyChangedEventArgs(property));
    }
  }
}
